<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class CommenController extends Controller
{
    public function beforeAction($action)
    {
        $path = 'admin';
        $model = $this->id;
        $action = Yii::$app->controller->action->id;
        if(empty($model) OR empty($action))
        {
            echo '模块或者方法不存在';
            return FALSE;
        }
        $access_arr = array();
		$request = Yii::$app->request;
		$role_id = $request->get('role_id');
		$sqlDb = new \yii\db\Query();
        $role_id = empty($role_id) ? 1 : intval($role_id) ;
        if($role_id == 1)
        {
            $access_arr = $sqlDb->from('node')->all();//超级管理员获取所有权限节点
        }
        else
        {
            //根据不同组来获取节点
            $access_arr = $sqlDb->select(array('node.*'))->where(array('role.id'=>$role_id,'role.status'=>1))->join('left join','access','access.role_id = role.id')->join('left join','node','access.node_id = node.id and access.pid = node.pid')->from('role')->all();
        }
        if(empty($access_arr))
        {
            echo '你没有权限';
            return FALSE;
        }
        $number = 0;
        foreach($access_arr as $key=>$val)
        {
            //后台入口权限
            if ($val['model'] === $path && $val['action'] === $path && intval($val['level']) === 1 ) 
            {
                $number ++;
            }
            //model权限
            if ($val['model'] === $model && $val['action'] === $model && intval($val['level']) === 2)   
                $number ++;
            //action权限
            if ($val['model'] === $model && $val['action'] === $action && intval($val['level']) === 3)  
                $number ++;
        }
        if($number !== 3)
        {
            echo '你没有权限等级不够';
            return FALSE;
        }        
        return TRUE;
    }
}
